package com.haidechizi.leetcode;

import java.util.LinkedList;

/**
 * @author: wangdejun
 * @create: 2020-04-17 12:28
 **/
public class _978_最长湍流子数组 {

    public int maxTurbulenceSize(int[] A) {
        LinkedList<Integer> queue = new LinkedList<>();
        String flag = null;// null,up,down
        queue.add(A[0]);
        int max = 1;
        for (int i = 1; i < A.length; i++) {
            Integer peek = queue.getLast();
            if (flag == null) {
                if (peek > A[i]) {
                    flag = "down";
                } else if (peek < A[i]) {
                    flag = "up";
                } else {
                    queue = new LinkedList<>();
                    /*while (!queue.isEmpty()) {
                        queue.poll();
                    }*/
                }
            } else if (flag == "up") {
                if (peek > A[i]) {
                    flag = "down";
                } else if (peek < A[i]) {
                    queue = new LinkedList<>();
                    queue.add(peek);
                    /*while (queue.size() != 1) {
                        queue.poll();
                    }*/
                    flag = "up";
                } else {
                    queue = new LinkedList<>();
                    /*while (!queue.isEmpty()) {
                        queue.poll();
                    }*/
                    flag = null;
                }
            } else {
                if (peek > A[i]) {
                    queue = new LinkedList<>();
                    queue.add(peek);
                    /*while (queue.size() != 1) {
                        queue.poll();
                    }*/
                    flag = "down";
                } else if (peek < A[i]) {
                    flag = "up";
                } else {
                    queue = new LinkedList<>();
                    /*while (!queue.isEmpty()) {
                        queue.poll();
                    }*/
                    flag = null;
                }
            }
            queue.add(A[i]);
            max = Math.max(max, queue.size());
        }
        return max;
    }

    public static void main(String[] args) {
        new _978_最长湍流子数组().maxTurbulenceSize(new int[]{
                9, 4, 2, 10, 7, 8, 8, 1, 9});
    }


    public int maxTurbulenceSize1(int[] A) {
        LinkedList<Integer> queue = new LinkedList<>();
        String flag = null;// null,up,down
        queue.add(A[0]);
        int max = 1;
        boolean ret = false;
        for (int i = 1; i < A.length; i++) {
            ret = false;
            Integer peek = queue.getLast();
            if (flag == null) {
                if (peek > A[i]) {
                    flag = "down";
                } else if (peek < A[i]) {
                    flag = "up";
                } else {
                    while (!queue.isEmpty()) {
                        queue.poll();
                    }
                }
            } else if (flag == "up") {
                if (peek > A[i]) {
                    flag = "down";
                } else if (peek < A[i]) {
                    while (queue.size() != 1) {
                        queue.poll();
                    }
                    flag = "up";
                } else {
                    while (!queue.isEmpty()) {
                        queue.poll();
                    }
                    flag = null;
                }
            } else {
                if (peek > A[i]) {
                    while (queue.size() != 1) {
                        queue.poll();
                    }
                    flag = "down";
                } else if (peek < A[i]) {
                    flag = "up";
                } else {
                    while (!queue.isEmpty()) {
                        queue.poll();
                    }
                    flag = null;
                }
            }
            queue.add(A[i]);
            max = Math.max(max, queue.size());
        }
        return max;
    }
}
